package com.cdxy.service.impl;

import com.cdxy.dmr.dataObject.AxUserDO;
import com.cdxy.dmr.dataObject.AxUserProfileDO;
import com.cdxy.dmr.repository.AxUserProfileRepository;
import com.cdxy.dmr.repository.AxUserRepository;
import com.cdxy.enums.*;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.req.LoginBindEmailReq;
import com.cdxy.model.dto.resp.UserInfoResp;
import com.cdxy.service.IBindService;
import com.cdxy.service.ISmsService;
import com.cdxy.util.AvatarUtil;
import com.cdxy.util.BeanUtil;
import com.cdxy.util.NicknameUtil;
import com.cdxy.util.UidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * @Author: tangy
 * @Date: 2020/3/6 18:21
 */
@Service
public class BindServiceImpl implements IBindService {

    @Autowired
    private AxUserRepository axUserRepository;

    @Autowired
    private AxUserProfileRepository axUserProfileRepository;

    @Autowired
    private ISmsService smsService;


    @Override
    public UserInfoResp loginBindEmail(LoginBindEmailReq loginBindEmailReq) {
        String email = loginBindEmailReq.getEmail();
        String phone = loginBindEmailReq.getPhone();
        String code = loginBindEmailReq.getPhoneCode();

        smsService.verifySms(KaptchaEnum.BusinessCode.LOGIN_BIND_EMAIL, phone, code);
        AxUserDO axUserEmailDO = axUserRepository.getByEmail(email);
        AxUserDO axUserMobileDO = axUserRepository.getByMobile(phone);

        if (axUserEmailDO != null) {
            throw new BusinessException(BizErrorEnum.EMAIL_HAD_BIND);
        }

        if (axUserMobileDO != null && StringUtils.isNotBlank(axUserMobileDO.getEmail())) {
            throw new BusinessException(BizErrorEnum.EMAIL_HAD_BIND);
        }

        if (axUserMobileDO != null) {
            Integer axUid = axUserMobileDO.getAxUid();
            AxUserDO axUserDO = new AxUserDO();
            axUserDO.setEmail(email);
            axUserDO.setAxUid(axUid);
            axUserRepository.updateById(axUserDO);

            return doAfterSuccessLogin(axUid);
        } else {
            AxUserDO axUserDO = new AxUserDO();
            axUserDO.setEmail(email);
            axUserDO.setMobile(phone);
            axUserDO.setRole(UserRoleEnum.NORMAL.name());
            axUserDO.setStatus(true);
            axUserRepository.save(axUserDO);

            Integer axUid = axUserDO.getAxUid();

            AxUserProfileDO axUserProfileDO = new AxUserProfileDO();
            axUserProfileDO.setAxUid(axUid);
            axUserProfileDO.setSex(UserSexEnum.UNKNOWN.getCode());
            axUserProfileDO.setNickname(NicknameUtil.buildDefaultNickName(UserTypeEnum.MOBILE, UidUtil.generateCodeByAxUid(axUid)));
            axUserProfileDO.setAvatar(AvatarUtil.buildDefaultAvatar(axUid));
            axUserProfileDO.setBirthDay(LocalDate.now());

            axUserProfileRepository.save(axUserProfileDO);

            return doAfterSuccessLogin(axUid);
        }

    }


    private UserInfoResp doAfterSuccessLogin(Integer axUid) {
        AxUserDO axUserDO = axUserRepository.getById(axUid);
        String mobile = axUserDO.getMobile();
        String role = axUserDO.getRole();

        AxUserProfileDO axUserProfileDO = axUserProfileRepository.getById(axUid);
        UserInfoResp userInfoResp = BeanUtil.copyPropertiesByFastJson(axUserProfileDO, UserInfoResp.class);
        userInfoResp.setMobile(mobile);
        userInfoResp.setRole(role);

        return userInfoResp;
    }
}
